import axios, { Method } from "axios";
import AccountStore, { UserInfo } from './AccountStore'

// 定义接口路径
enum RequestPath {
    login = "/users/login",
    signup = "/users/signup",
    operationalAdd = "/operational/add",
    operationalGet = "/operational/get",
    operationalUpdate = "/operational/update",
    goodsCategoryGet = "/goods/category/get",
    goodsCategoryAdd = "/goods/category/add",
    goodsCategoryUpdate = "/goods/category/update",
    goodsGet = "/goods/get",
    goodsListGet = "/goods/list/get",
    goodsAdd = "/goods/add",
    goodsUpdate = "/goods/update",
    goodsDelete = "/goods/delete",
    orderListGet = '/order/list/get',
    orderUpdate = '/order/update',
    evaluationUpdate = '/evaluation/update',
    evaluationListGet = '/evaluation/list/get',
    statisticalGet = '/statistical/get'
}
// 创建一个请求worker实例
const worker = axios.create({
    baseURL: 'http://localhost:3000/api/v1',
    timeout: 5000,
    headers: {}
})
// 定义个拦截器
worker.interceptors.response.use((response) => {
    return response;
}, (error)=>{
    if (error.response.status == 401) {
        // 直接退出登录
        AccountStore().logout()
        window.location.href = '/#/login'
    }
    return Promise.reject(error);
})

// 定义登录注册的接口数据返回结构
export interface LoginResponseData {
    msg: 'error' | 'ok',
    error?: string,
    info: UserInfo,
    token?: string
}

export interface OperationalItemData {
    id: number,
    name: string,
    status: number,
    created_at: string,
    cover: string,
    content: string,
    uri: string
}

export interface OperationalResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: OperationalItemData[]
}

export interface CategoryItemData {
    id: number,
    name: string,
    description: string,
    created_at: string,
    sort: number
}

export interface CategroyResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: CategoryItemData[]
}

export interface GoodsItemData {
    id: number,
    name: string,
    description: string,
    created_at: string,
    detail?: string,
    price: number,
    discounted_price: number,
    stock: number,
    image: string,
    status: number,
    brand: string,
    category: CategoryItemData
}

export interface GoodsResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: GoodsItemData[]
}

// 购物车中的商品信息模型
export interface ShopCarGoodsData {
    count: number,
    item: GoodsItemData
}

// 订单模型
export interface OrderData {
    id: number,
    user_id: number,
    goods?: ShopCarGoodsData[],
    address: string,
    price: number,
    save_price:number,
    state: 0 | 1 | 2 | 3,
    created_at: string
}
// 订单接口返回的数据结构模型
export interface OrderResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: OrderData[]
}

// 评价模型
export interface EvaluationItemData {
    id: number,
    user: UserInfo,
    gid: number,
    content: string,
    star: number,
    created_at: string,
    state: number
}

export interface EvaluationResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: EvaluationItemData[]
}

// 数据统计模型
export interface StatisticalItemData {
    allCount: number,
    datas:{date: string, count: number}[]
}

export interface StatisticalResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: StatisticalItemData[]
}

// 发起请求的方法
function startRequest(path: RequestPath, method: Method, params: any) {
    let config:any = {
        method: method,
        url: path
    }
    // get方法将参数放在params中
    if (method == 'GET' || method == 'get') {
        config.params = params
    // post方法将参数放在data中
    } else {
        config.data = params
    }
    // 会返回一个pormiss
    return worker.request(config)
}
// 导出
export {
    worker,
    RequestPath,
    startRequest
}

